using AutoMapper;
using Core.Db.Dtos;
using Core.Db.Models;
using Core.Db.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Core.API.Controllers;

[Route("/api/touristRoutes/{touristRouteId}/pictures")]
[ApiController]
public class TouristRoutePicturesController : ControllerBase
{
    private readonly ITouristRouteRepository _touristRouteRepository;
    private readonly IMapper _mapper;

    public TouristRoutePicturesController(ITouristRouteRepository touristRouteRepository, IMapper mapper)
    {
        _touristRouteRepository = touristRouteRepository;
        _mapper = mapper;
    }

    [HttpGet(Name = "GetPictureListForTouristRoute")]
    public async Task<IActionResult> GetPictureListForTouristRoute(Guid touristRouteId)
    {
        if (await _touristRouteRepository.TouristRouteExistAsync(touristRouteId) == false)
        {
            return NotFound("旅游线路不存在");
        }

        var pictureFromRepo = await _touristRouteRepository.GetPicturesByTouristRouteIdAsync(touristRouteId);
        if (pictureFromRepo == null || pictureFromRepo.Count() == 0)
        {
            return NotFound("照片不存在");
        }

        var pictureDto = _mapper.Map<IEnumerable<TouristRoutePictureDto>>(pictureFromRepo);
        return Ok(pictureDto);
    }

    [HttpGet("{pictureId}", Name = "GetPicture")]
    public async Task<IActionResult> GetPicture(Guid touristRouteId, int pictureId)
    {
        if (await _touristRouteRepository.TouristRouteExistAsync(touristRouteId) == false)
        {
            return NotFound("旅游线路不存在");
        }

        var pictureFromRepo = await _touristRouteRepository.GetPictureAsync(pictureId);
        if (pictureFromRepo == null)
        {
            return NotFound("图片不存在");
        }

        var pictureDto = _mapper.Map<TouristRoutePictureDto>(pictureFromRepo);
        return Ok(pictureDto);
    }

    [Authorize(AuthenticationSchemes = "Bearer")]
    [Authorize(Roles = "Admin")]
    [HttpPost(Name = "CreateTouristRoutePicture")]
    public async Task<IActionResult> CreateTouristRoutePicture([FromRoute] Guid touristRouteId, [FromBody] TouristRoutePictureForCreationDto touristRoutePictureForCreationDto)
    {
        if (await _touristRouteRepository.TouristRouteExistAsync(touristRouteId) == false)
        {
            return NotFound("旅游线路不存在");
        }

        var pictureModel = _mapper.Map<TouristRoutePicture>(touristRoutePictureForCreationDto);
        await _touristRouteRepository.AddTouristRoutePictureAsync(touristRouteId, pictureModel);
        await _touristRouteRepository.SaveAsync();
        var pictureToReturn = _mapper.Map<TouristRoutePictureDto>(pictureModel);
        return CreatedAtRoute("GetPicture", new { touristRouteId = pictureModel.TouristRouteId, pictureId = pictureModel.Id }, pictureToReturn);
    }

    [Authorize(AuthenticationSchemes = "Bearer")]
    [Authorize(Roles = "Admin")]
    [HttpDelete("{pictureId}")]
    public async Task<IActionResult> DeletePicture([FromRoute] Guid touristRouteId, [FromRoute] int pictureId)
    {
        if (await _touristRouteRepository.TouristRouteExistAsync(touristRouteId) == false)
        {
            return NotFound("旅游线路不存在");
        }

        var pictureFromRepo = await _touristRouteRepository.GetPictureAsync(pictureId);
        await _touristRouteRepository.DeleteTouristRoutePictureAsync(pictureFromRepo);
        await _touristRouteRepository.SaveAsync();
        return NoContent();
    }
}